package com.zhangyj.component.tags;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import com.zhangyj.util.BeanParse;

public class BaseTag extends AbstractTag{
	private static final long serialVersionUID = 2763600738735303386L;
	PageContext pageContext;
	private Tag parent;

	private String name, style, value, onblur, onclick, ondblclick, onfocus, onhelp, onkeydown, onkeypress, onkeyup, onmousedown,
			onmousemove, onmouseout, onmouseover, onmouseup, onresize, onselect;

	private void writeEvent(Writer writer, String name, String value) throws IOException {
		if (value == null || value.length() == 0) {
			return;
		}
		writer.append(" ");
		writer.append(name);
		writer.append("=\"_sys_");
		writer.append(name);
		writer.append("(event)\" u_");
		writer.append(name);
		writer.append("=\"");
		writer.append(value);
		writer.append("\"");
	}

	Object getObjectFromKey(String val) throws Exception {

		if (val == null || val.length() == 0) {
			return "";
		}
		if (!val.startsWith("$")) {
			return val;
		}
		val = val.substring(1);
		if (val.length() == 0) {
			return "";
		}
		String key = null;
		String propertiy = null;
		if (val.indexOf('.') > -1) {
			key = val.substring(0, val.indexOf('.') + 1);
			propertiy = val.substring(val.indexOf('.') + 1);
		} else {
			key = val;
		}

		Object o = null;
		o = this.pageContext.getAttribute(key);
		if (o == null) {
			o = this.pageContext.getRequest().getAttribute(key);
		}
		if (o == null) {
			o = this.pageContext.getServletContext().getAttribute(key);
		}

		if (propertiy != null) {
			o = BeanParse.getValue(o, propertiy);
		}
		if (o != null) {
			return o;
		}
		return "";
	}

	void writerValue(Writer writer, String name, String value) throws IOException {
		if (value != null && value.length() > 0) {
			writer.append(" " + name + "=\"");
			writer.append(value);
			writer.append("\"");
		}
	}

	void writerPublic(Writer writer) throws Exception {
		writerValue(writer, "name", this.name);
		writerValue(writer, "id", this.name);
		writerValue(writer, "style", style);
		writeEvent(writer, "onblur", this.onblur);
		writeEvent(writer, "onclick", this.onclick);
		writeEvent(writer, "ondblclick", this.ondblclick);
		writeEvent(writer, "onfocus", this.onfocus);
		writeEvent(writer, "onhelp", this.onhelp);
		writeEvent(writer, "onkeydown", this.onkeydown);
		writeEvent(writer, "onkeyup", this.onkeyup);
		writeEvent(writer, "onmousedown", this.onmousedown);
		writeEvent(writer, "onmousemove", this.onmousemove);
		writeEvent(writer, "onmouseout", this.onmouseout);
		writeEvent(writer, "onmouseover", this.onmouseover);
		writeEvent(writer, "onmouseup", this.onmouseup);
		writeEvent(writer, "onresize", this.onresize);
		writeEvent(writer, "onselect", this.onselect);

	}

	
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	
	public int doStartTag() throws JspException {
		return EVAL_BODY_INCLUDE;
	}

	
	public Tag getParent() {
		return this.parent;
	}

	
	public void release() {
	}

	
	public void setPageContext(PageContext pageContext) {
		this.pageContext = pageContext;
	}

	
	public void setParent(Tag parent) {
		this.parent = parent;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getOnblur() {
		return onblur;
	}

	public void setOnblur(String onblur) {
		this.onblur = onblur;
	}

	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	public String getOndblclick() {
		return ondblclick;
	}

	public void setOndblclick(String ondblclick) {
		this.ondblclick = ondblclick;
	}

	public String getOnfocus() {
		return onfocus;
	}

	public void setOnfocus(String onfocus) {
		this.onfocus = onfocus;
	}

	public String getOnhelp() {
		return onhelp;
	}

	public void setOnhelp(String onhelp) {
		this.onhelp = onhelp;
	}

	public String getOnkeydown() {
		return onkeydown;
	}

	public void setOnkeydown(String onkeydown) {
		this.onkeydown = onkeydown;
	}

	public String getOnkeypress() {
		return onkeypress;
	}

	public void setOnkeypress(String onkeypress) {
		this.onkeypress = onkeypress;
	}

	public String getOnkeyup() {
		return onkeyup;
	}

	public void setOnkeyup(String onkeyup) {
		this.onkeyup = onkeyup;
	}

	public String getOnmousedown() {
		return onmousedown;
	}

	public void setOnmousedown(String onmousedown) {
		this.onmousedown = onmousedown;
	}

	public String getOnmousemove() {
		return onmousemove;
	}

	public void setOnmousemove(String onmousemove) {
		this.onmousemove = onmousemove;
	}

	public String getOnmouseout() {
		return onmouseout;
	}

	public void setOnmouseout(String onmouseout) {
		this.onmouseout = onmouseout;
	}

	public String getOnmouseover() {
		return onmouseover;
	}

	public void setOnmouseover(String onmouseover) {
		this.onmouseover = onmouseover;
	}

	public String getOnmouseup() {
		return onmouseup;
	}

	public void setOnmouseup(String onmouseup) {
		this.onmouseup = onmouseup;
	}

	public String getOnresize() {
		return onresize;
	}

	public void setOnresize(String onresize) {
		this.onresize = onresize;
	}

	public String getOnselect() {
		return onselect;
	}

	public void setOnselect(String onselect) {
		this.onselect = onselect;
	}

}
